//
//  ViewController.m
//  KEEP
//
//  Created by ZhangXu on 16/3/29.
//  Copyright © 2016年 zhangXu. All rights reserved.
//

#import "CDViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
@interface CDViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)MPMoviePlayerController *moviePlayer;
@property(nonatomic ,strong)NSTimer *timer;
@property(nonatomic ,strong)AVAudioSession *avaudioSession;

@property (nonatomic,weak) UIButton *registButton;
@property (nonatomic,weak) UIButton *loginButton;

@property (nonatomic,weak) UIPageControl *pageControl;
@property (nonatomic,weak) UIImageView *iconView;
@property (nonatomic,weak) UIScrollView *scrollView;
@end

@implementation CDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    /**
     *  设置其他音乐软件播放的音乐不被打断
     */
    
    self.avaudioSession = [AVAudioSession sharedInstance];
    NSError *error = nil;
    [self.avaudioSession setCategory:AVAudioSessionCategoryAmbient error:&error];
    
    
    
    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"3.mp4" ofType:nil];
    
    NSURL *url = [NSURL fileURLWithPath:urlStr];
    
    _moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
    //    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    [_moviePlayer play];
    [_moviePlayer.view setFrame:self.view.bounds];
    
    [self.view addSubview:_moviePlayer.view];
    _moviePlayer.shouldAutoplay = YES;
    [_moviePlayer setControlStyle:MPMovieControlStyleNone];
    [_moviePlayer setFullscreen:YES];
    
    [_moviePlayer setRepeatMode:MPMovieRepeatModeOne];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackStateChanged)
                                                 name:MPMoviePlayerPlaybackStateDidChangeNotification
                                               object:_moviePlayer];
    
    [self setupUI];
    
    [self setupTimer];

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)setupUI{
    [self addTwoButtons];
    [self addPageControl];
    [self addIconView];
    [self addScrollView];
}

-(void)addTwoButtons{
    UIButton *registButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [registButton setTitle:@"注册" forState:UIControlStateNormal];
    [loginButton setTitle:@"登录" forState:UIControlStateNormal];
    
    [registButton addTarget:self action:@selector(registButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [loginButton addTarget:self action:@selector(loginButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [registButton setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.5]];
    [loginButton setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.5]];
    [loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.registButton = registButton;
    self.loginButton = loginButton;
    
    [self.view addSubview:registButton];
    [self.view addSubview:loginButton];
    //约束
    [registButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.bottom.offset(-15);
        make.height.offset(40);
        make.width.offset((CDScreen.size.width - 15 - 15 - 15) / 2);
    }];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.bottom.offset(-15);
        make.left.equalTo(registButton.mas_right).offset(15);
        make.height.equalTo(registButton);
    }];
}

-(void)addPageControl{
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    
    pageControl.numberOfPages = 4;
    
    pageControl.pageIndicatorTintColor = [UIColor colorWithHex:0x797979];
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithHex:0x35cd9d];
    
    self.pageControl = pageControl;
    
    [self.view addSubview:pageControl];
    
    //约束
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.equalTo(self.registButton.mas_top).offset(0);
        make.width.offset(100);
    }];
}

-(void)addIconView{
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"HomeLogo.png"]];
    
    self.iconView = iconView;
    
    [self.view addSubview:iconView];
    
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.centerY.offset(-60);
    }];
}

-(void)addScrollView{
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    
    scrollView.contentSize = CGSizeMake(CDScreen.size.width * 4, 0);
    scrollView.pagingEnabled = YES;
    
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;

    scrollView.bounces = NO;
    
    scrollView.delegate = self;
    
    self.scrollView = scrollView;
    
    [self addLabelText:@"好奇心是人类最美好的品质之一" index:0];
    [self addLabelText:@"我们提供最优价值的信息" index:1];
    [self addLabelText:@"我们分享全球最棒的想法" index:2];
    [self addLabelText:@"我们关注最实时的各界动态" index:3];
    
    [self.view addSubview:scrollView];
    
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.equalTo(self.registButton.mas_top).offset(0);
    }];
}

-(void)addLabelText:(NSString *)text index:(NSInteger)index{
    UILabel *label = [[UILabel alloc] init];
    
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:18];
    label.textAlignment = NSTextAlignmentCenter;
    
    label.text = text;
    
    [self.scrollView addSubview:label];
    
    CGFloat labelY = CDScreen.size.height - 135;
    label.frame = CGRectMake(index * CDScreen.size.width, labelY, CDScreen.size.width, 50);
}

#pragma mark -- 按钮点击事件
-(void)registButtonClick:(UIButton *)sender{

}

-(void)loginButtonClick:(UIButton *)sender{

}

-(void)playbackStateChanged{
    //取得目前状态
    MPMoviePlaybackState playbackState = [_moviePlayer playbackState];
    
    //状态类型
    switch (playbackState) {
        case MPMoviePlaybackStateStopped:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStatePlaying:
            NSLog(@"播放中");
            break;
            
        case MPMoviePlaybackStatePaused:
            [_moviePlayer play];
            break;
            
        case MPMoviePlaybackStateInterrupted:
            NSLog(@"播放被中断");
            break;
            
        case MPMoviePlaybackStateSeekingForward:
            NSLog(@"往前快转");
            break;
            
        case MPMoviePlaybackStateSeekingBackward:
            NSLog(@"往后快转");
            break;
            
        default:
            NSLog(@"无法辨识的状态");
            break;
    }
}

//ios以后隐藏状态栏
-(BOOL)prefersStatusBarHidden{
    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

-(void)setupTimer{
    
    self.timer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(timerChanged) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

-(void)pageChanged:(UIPageControl *)pageControl{
    
    CGFloat x = (pageControl.currentPage) * [UIScreen mainScreen].bounds.size.width;
    
    [self.scrollView setContentOffset:CGPointMake(x, 0) animated:YES];
    
    
}

-(void)timerChanged{
    int page  = (self.pageControl.currentPage +1) %4;
    
    self.pageControl.currentPage = page;
    
    [self pageChanged:self.pageControl];
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    double page = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;
    self.pageControl.currentPage = page;
    
    if (page== - 1)
    {
        self.pageControl.currentPage = 3;// 序号0 最后1页
    }
    else if (page == 4)
    {
        self.pageControl.currentPage = 0; // 最后+1,循环第1页
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.timer invalidate];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    [self setupTimer];
    
}

@end